package org.mtdownload.dialogs.checklist;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.WindowConstants;
import javax.swing.table.TableColumnModel;


public class CheckListDialog extends JDialog{
	private static final long serialVersionUID = 3257846571722159414L;
	private JScrollPane scroll=null;
	private JTable table=null;
	private static CheckListTableModel model=null;	
	private static boolean cancelled=true;
	
	private CheckListDialog(Frame owner,String title,List<String> l,String message) {
		super(owner,title,true);
		init(l,message);
	}

	private void init(List<String> l,String message) {		
		model=new CheckListTableModel(l); 		
		table=new JTable(model) {   
			private static final long serialVersionUID = 3258409551706273842L;
			//Implement table cell tool tips.	
			public String getToolTipText(MouseEvent e) {
				String tip = null;
				java.awt.Point p = e.getPoint();
				int rowIndex = rowAtPoint(p);
				int colIndex = columnAtPoint(p);
				int realColumnIndex = convertColumnIndexToModel(colIndex);
				
				if (realColumnIndex == 1) { //URL column
					tip = (String)getValueAt(rowIndex, colIndex);
				} else { //another column
					tip = super.getToolTipText(e);
				}
				return tip;
			}
		};
		scroll=new JScrollPane(table);		
		TableColumnModel colModel=table.getColumnModel();
		colModel.getColumn(0).setPreferredWidth(35);
		colModel.getColumn(0).setMaxWidth(35);

		JPanel listPane=new JPanel();
		listPane.setLayout(new BoxLayout(listPane, BoxLayout.PAGE_AXIS));
        JLabel label = new JLabel(message);
        label.setLabelFor(table);
        listPane.add(label);
        listPane.add(Box.createRigidArea(new Dimension(0,5)));
        listPane.add(scroll);
        listPane.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
		
		JButton okButton=new JButton("OK");
		okButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				cancelled=false;
				dispose();
			}
		});
		JButton cancelButton=new JButton("Cancel");
		cancelButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				cancelled=true;
				dispose();
			}
		});		
        //Lay out the buttons from left to right.
        JPanel buttonPane = new JPanel();
        buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT,10,0));
        buttonPane.setBorder(BorderFactory.createEmptyBorder(0, 10, 10, 10));               
        buttonPane.add(okButton);		
		buttonPane.add(cancelButton);        
		getRootPane().setDefaultButton(okButton);
		setSize(new Dimension(400,250));
		setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		JPanel contentPane=(JPanel)getContentPane();		
		contentPane.setLayout(new BorderLayout());
		contentPane.add(listPane,BorderLayout.CENTER);
		contentPane.add(buttonPane,BorderLayout.SOUTH);
	}


	/**
	 * This method shows a dialog that contains a list of values and the user can select or deselect
	 * them. It returns a new list with the selected values, or null if the dialog has been cancelled. 
	 * @param parent
	 * @param message
	 * @param title
	 * @param list
	 * @return
	 */
	public static List showCheckListDialog(Component parent,String message,String title,List<String> list){
		CheckListDialog d=new CheckListDialog(JOptionPane.getFrameForComponent(parent),title,list,message);
		d.setLocationRelativeTo(parent);
		d.setVisible(true);
		if(cancelled){
			return null;
		}
		return model.getSelectedValues();
	}
}
